package com.live.stamp.web.oss.service.impl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.aliyuncs.sts.model.v20150401.AssumeRoleRequest;
import com.aliyuncs.sts.model.v20150401.AssumeRoleResponse;
import com.live.stamp.web.oss.service.OssService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.container.core.common.BusinessException;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author cwd E-mail: wendong.cao@jsxfedu.com
 * @version 1
 * @date 2018/4/18 15:07
 */
@Service
public class OssServiceImpl implements OssService {

    @Value("${oss.accessKeyID}")
    private String accessKeyID;
    @Value("${oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${oss.roleArn}")
    private String roleArn;
    @Value("${oss.tokenExpireTime}")
    private Long tokenExpireTime;
    @Value("${oss.policyFile}")
    private String policyFile;
    @Value("${oss.regionId}")
    private String regionId;
    @Value("${oss.stsApiVersion}")
    private String stsApiVersion;
    @Value("${oss.roleSessionName}")
    private String roleSessionName;


    @Override
    public Map<String, Object> getStsToken() throws BusinessException {
        AssumeRoleResponse stsResponse = null;
        try {
            stsResponse = assumeRole();
            Map<String, Object> respMap = new LinkedHashMap<>();
            respMap.put("StatusCode", "200");
            respMap.put("AccessKeyId", stsResponse.getCredentials().getAccessKeyId());
            respMap.put("AccessKeySecret", stsResponse.getCredentials().getAccessKeySecret());
            respMap.put("SecurityToken", stsResponse.getCredentials().getSecurityToken());
            respMap.put("Expiration", stsResponse.getCredentials().getExpiration());
            return respMap;
        } catch (ClientException e) {
            e.printStackTrace();
            throw new BusinessException();
        }
    }

    public AssumeRoleResponse assumeRole() throws ClientException {
        try {
            // 创建一个 Aliyun Acs Client, 用于发起 OpenAPI 请求
            IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret);
            DefaultAcsClient client = new DefaultAcsClient(profile);

            // 创建一个 AssumeRoleRequest 并设置请求参数
            final AssumeRoleRequest request = new AssumeRoleRequest();
//            request.setVersion(stsApiVersion);
            request.setMethod(MethodType.POST);
            request.setProtocol(ProtocolType.HTTPS);

            request.setRoleArn(roleArn);
            request.setRoleSessionName(roleSessionName);
//            request.setPolicy(policyFile);
            request.setDurationSeconds(tokenExpireTime);

            // 发起请求，并得到response
            final AssumeRoleResponse response = client.getAcsResponse(request);

            return response;
        } catch (ClientException e) {
            e.printStackTrace();
            throw e;
        }
    }
}
